package com.sumer.psychology.blog.infrastructure.persistence.redis;

import com.google.gson.Gson;
import com.mongodb.DBObject;
import com.sumer.psychology.blog.domain.model.BlogAvailability;
import com.sumer.psychology.blog.infrastructure.persistence.mongo.MongoBlogRepository;
import com.sumer.psychology.common.notification.NotificationReader;
import com.sumer.psychology.common.persistence.redis.RedisUtil;

import javax.inject.Inject;

/**
 * 负责监听创建博客  删除的动作，作为计算用户记录的积分只用；
 * ClassName: MongoCategoryListRepositoryForRedisQueue <br/>
 * date: 2015年8月22日 下午11:10:11 <br/>
 *
 * @author wangxiaolei
 * @since JDK 1.7
 */
public class RedisBlogRepository {
    private static final String DRM_BLOG_NOTIFY_QUEUE_NAME = "DRM:BLOG_NOTIFY_QUEUE";

    @Inject
    private RedisUtil redisUtil;

    @Inject
    private MongoBlogRepository mongoBlogRepository;

    /**
     * 增加
     *
     * @param data json
     */
    public void add(String data) {
        NotificationReader reader = new NotificationReader(data);
        String authorId = reader.eventStringValue("author.identity");
        String blogId = reader.eventStringValue("blogId.identity");

        //组织 添加 博文的通知信息
        SpaceDTO dto = new SpaceDTO(authorId, blogId, 1, 1);
        redisUtil.listPushTail(DRM_BLOG_NOTIFY_QUEUE_NAME, new Gson().toJson(dto));
    }


    /**
     * 删除
     *
     * @param blogId blog id
     */
    public void remove(String blogId) {
        DBObject blog = mongoBlogRepository.findOne(blogId, BlogAvailability.DEACTIVE);
        DBObject author = (DBObject) blog.get("author");

        //组织 添加 博文的通知信息
        SpaceDTO dto = new SpaceDTO((String) author.get("identity"), blogId, -1, 1);
        redisUtil.listPushTail(DRM_BLOG_NOTIFY_QUEUE_NAME, new Gson().toJson(dto));
    }
}
